package com.group2.view;

import javax.swing.JPanel;

import java.awt.*;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.Socket;

import model.*;
import static model.netBase.IP;
public class BankBonusFrame extends JPanel {
	private JTextField BonusName;
	private JTextField BonusMoney;
	private JTextField Id;
	private JTextField Name;

	/**
	 * Create the panel.
	 */
	public BankBonusFrame() {
		setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u5956\u5B66\u91D1\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					BonusActionPerformed(e);
				} catch (IOException | ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed();
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(54, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addGap(40)
							.addComponent(btnNewButton_1)
							.addGap(131))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
							.addGap(47))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(33, Short.MAX_VALUE)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(18))
		);
		
		JLabel lblNewLabel = new JLabel("姓名：");

		JLabel lblNewLabel_1 = new JLabel("学号：");
		
		JLabel lblNewLabel_2 = new JLabel("奖学金金额：");
		
		JLabel lblNewLabel_3 = new JLabel("奖学金名称：");
		
		BonusName = new JTextField();
		BonusName.setColumns(10);

		BonusMoney = new JTextField();
		BonusMoney.setColumns(10);
		
		Id = new JTextField();
		Id.setColumns(10);
		
		Name = new JTextField();
		Name.setColumns(10);
		Name.setForeground(Color.gray); //将提示文字设置为灰色
		Name.setText("选填");
		Name.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				if (Name.getText().equals("选填")) {
					Name.setText("");     //将提示文字清空
					Name.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
				}
			}
			@Override
			public void focusLost(FocusEvent e) {
				if (Name.getText().equals("")){
					Name.setForeground(Color.gray); //将提示文字设置为灰色
					Name.setText("选填");     //显示提示文字
				}
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(58)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_3)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(BonusName))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(Name, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
								.addComponent(Id, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
								.addComponent(BonusMoney))))
					.addContainerGap(80, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(Name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(Id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(BonusMoney, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(BonusName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(35, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);

	}
	/**
	 * 奖学金发放事件处理
	 */
	private void BonusActionPerformed(ActionEvent e) throws IOException, ClassNotFoundException {
		String id=this.Id.getText();
		String bonusName=this.BonusName.getText();
		String bonusMoney=this.BonusMoney.getText();
		System.out.println(bonusName);
		if(id==""||bonusName==""||bonusMoney=="") {
			JOptionPane.showMessageDialog(null,"未输入完整信息！");
			return;
		}
		//连接数据库
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("31" + '\n'+id+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream ois=new ObjectInputStream(is);
		Object obj=ois.readObject();
		Student student= (Student) obj;
		String name=student.getName();
		int n = JOptionPane.showConfirmDialog(this, "确定发放给"+name+"吗?");
		if(n==0) {
			System.out.println("开始传输");
			Socket socket1 = new Socket(IP(), 2000);//此处主机地址请自行修改
			//设置超时时间
			socket.setSoTimeout(3000);
			//构建IO
			OutputStream os1 = socket1.getOutputStream();
			InputStream is1=socket1.getInputStream();
			BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(os1));
			//构建IO
			bw1.write("65" + '\n'+id+'\n'+bonusMoney+'\n'+bonusName+'\n');
			bw1.flush();
			//从服务端输入到本地
			BufferedReader br=new BufferedReader(new InputStreamReader(is1));
			String str=br.readLine();
			System.out.println("传输结束");
			if (str.equals("true")) {JOptionPane.showMessageDialog(this, "发放成功!");
				resetValueActionPerformed();
			} else {
				JOptionPane.showMessageDialog(this, "发放失败!");
			}
		}
		else JOptionPane.showMessageDialog(this, "取消成功");


		
	}
	/**
	 * 重置事件处理
	 */
	private void resetValueActionPerformed() {
		this.resetValue();
	}
	/**
	 * 清空内容（重置）
	 */
	private void resetValue() {
		this.Name.setText("");
		this.Id.setText("");
		this.BonusName.setText("");
		this.BonusMoney.setText("");
	}

}
